package com.onesports.rabbitmq.message;

import com.onesports.common.MessageUtil;
import com.onesports.common.model.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * @Author: HUANG GY.
 * @Date: 2020/6/19 16:18
 * @Description: com.onesports.rabbitmq.message
 */
public abstract class AbstractMessageSend implements IMessageSend {

    @Autowired
    private RabbitTemplate rabbitTemplate ;

    @Value("${spring.rabbitmq.exchange-name}")
    private String exchageName ;

    @Override
    public void send(Message message) {
        if (message.getMsgId() == null) {
            message.setMsgId(MessageUtil.createMsgId());
        }
        if (message.getCreateTime() != null) {
            message.setCreateTime( MessageUtil.getNowTime());
        }
        CorrelationData correlationData = new CorrelationData(message.getMsgId());
        this.persistence(message);
        this.rabbitTemplate.convertAndSend(exchageName, null, message,correlationData);
    }

    /**
     *  持久化
     * @param message
     */
    public abstract void persistence(Message message);
}
